package org.pp.controller;


import io.swagger.v3.oas.annotations.Operation;
import org.pp.admin.dto.*;
import org.pp.annotations.Permission;
import org.pp.core.PageBean;
import org.pp.core.Result;
import org.pp.entity.SysUser;
import org.pp.service.ISysUserService;
import org.pp.vo.AdminSysUserVo;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author haima
 * @since 2024-04-20
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController
        extends  BaseCrudController<SysUser, SysUserAdd, SysUserEdite, AdminSysUserVo>
    implements BaseAdd<SysUserAdd>,
        BaseDelete,
        BaseEdite<SysUserEdite>,
        BasePage<AdminSysUserVo>
{
    @Autowired
    ISysUserService userService;
    @Override
    @Permission("post:/sys/user/add")
    public Result<Object> add(@RequestBody @Validated SysUserAdd dto) {
//        dto.setPassword(AdminSessionUtil.encrypt(dto.getPassword()));
//        SysUser user = userService.getOne(new LambdaQueryWrapper<SysUser>()
//                .eq(SysUser::getUserName, dto.getUserName()));
//        Assertion.notNullThrow(user,"用户名已经存在");
//        return super.add(dto);
        Long userId=  userService.add(dto);
        return Result.ok(userId);
    }

    @Override
    @Permission("post:/sys/user/delete")
    public Result<Void> delete(String ids) {
        //return super.delete(ids);
        userService.delete(ids);
        return Result.ok();
    }

    @Override
    @Permission("post:/sys/user/edite")
    public Result<Void> edite(SysUserEdite dto) {
       /* Long userId = AdminSessionUtil.getUserId();
        Assertion.equalsThrow(dto.getUserId(),userId,"不能编辑自己");
        return super.edite(dto);*/
        userService.edite(dto);
        return Result.ok();
    }


    //完全是一个新方法，不是继承而来
    @Operation(summary = "CRUD操作 分页查询")
    @GetMapping("/list")
    @Permission("get:/sys/user/list")
    public Result<PageBean<AdminSysUserVo>> page(@ParameterObject SysUserPage page) {
        //return super.page(page);
        return  Result.ok(userService.pageList(page));
    }

    @PostMapping("/password/reset")
    @Operation(summary = "给系统用户重置密码")
    @Permission("post:/sys/user/password/reset")
    public Result<Void> passwordReset(@RequestBody @Validated SysUserPasswordReset dto){
        userService.passwordReset(dto);
        return Result.ok();
    }

    @PostMapping("/status/switch")
    @Operation(summary = "修改系统用户状态 禁用与激活")
    @Permission("post:/sys/user/status/switch")
    public Result<Void> statusSwitch(@RequestBody @Validated SysUserStatusSwitch dto){
        userService.statusSwitch(dto);
        return Result.ok();
    }

}
